package com.example.fragment;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.RoomSQLiteQuery;
import androidx.room.util.DBUtil;

import com.example.activity.MainActivity;
import com.example.adapter.StudentListAdapter;
import com.example.room.ZRoomDB;
import com.example.room.user.User;
import com.example.xc.R;
import com.example.room.DbManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 同学列表适配器
 */
public class StudentListFragment extends Fragment {

    StudentListAdapter adapter;

    View rootView;

    /**
     *  本方法用于创建Fragment布局
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.new_fragment,container,false);            //关联布局文件
        return rootView;
    }

    /**
     *  本方法用于显示页面时刷新数据
     */
    @Override
    public void onResume() {
        super.onResume();
        initRecyclerView(rootView);
        MainActivity.initHomeBtnColor();
    }

    /**
     * 本方法用于实现RecyclerView+StaggeredGridLayout流式列表效果
     * @param view 传入新品布局对象
     */
    @SuppressLint({"Range", "RestrictedApi"})
    private void initRecyclerView(View view){
        List<User> desks=new ArrayList<>();
        RecyclerView rv=view.findViewById(R.id.newRecycler);                                        //获得RecyclerView控件对象
                                                                                                    //设置为StaggeredGridLayout流式布
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
                                                                                                    //定义新品小吃图片数

        Cursor cursor=null;
        try {
            ZRoomDB zRoomDB = DbManager.INSTANCE.getDB(requireContext());
            final String _sql = "SELECT * FROM USER";
            final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
            zRoomDB.assertNotSuspendingTransaction();
            cursor = DBUtil.query(zRoomDB, _statement, false, null);
            if (cursor == null) {
                return;
            }
            while (cursor.moveToNext()){
                int id=cursor.getInt(cursor.getColumnIndex("id"));
                String account=cursor.getString(cursor.getColumnIndex("account"));
                String password=cursor.getString(cursor.getColumnIndex("password"));
                String xingming=cursor.getString(cursor.getColumnIndex("xingming"));
                String banji=cursor.getString(cursor.getColumnIndex("banji"));
                String number=cursor.getString(cursor.getColumnIndex("number"));
                desks.add(new User(id,account,password,xingming,banji,number));
            }
        }catch (Exception e){
            Log.e("NewFragment","error"+e);
        }finally {
            if(cursor!=null){
                cursor.close();
            }
        }
        adapter=new StudentListAdapter(getActivity(),desks);                                //调用适配器
        rv.setAdapter(adapter);                                                     //设置适配器到RecyclerView
    }
}
